package org.feng.support;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.feng.entity.PubResource;
import org.feng.service.IPubResourceService;
import org.feng.support.impl.Minio;
import org.feng.support.impl.QiNiu;
import org.feng.support.impl.AliOss;
import org.springframework.stereotype.Component;


/**
 * 上传支持
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@Component
public class UploadSupport {
    @Resource
    private IPubResourceService sysResourceService;

    private UploadInterface uploadInterface;

    @PostConstruct
    private void initUploadManage() {
        PubResource pubResource = sysResourceService.getOne(Wrappers.<PubResource>lambdaQuery().eq(PubResource::getStatus, true));
        this.configUpload(pubResource);
    }

    /**
     * 更新上传实现类
     */
    public void updateUploadImpl() {
        PubResource pubResource = sysResourceService.getOne(Wrappers.<PubResource>lambdaQuery().eq(PubResource::getStatus, true));
        this.configUpload(pubResource);
    }

    /**
     * 匹配实现类
     *
     * @param pubResource
     */
    private void configUpload(PubResource pubResource) {
        if (pubResource == null) {
            this.uploadInterface = null;
        } else {
            if ("minio".equals(pubResource.getName())) {
                this.uploadInterface = new Minio(pubResource);
            } else if ("aliOss".equals(pubResource.getName())) {
                this.uploadInterface = new AliOss(pubResource);
            } else if ("qiniu".equals(pubResource.getName())) {
                this.uploadInterface = new QiNiu(pubResource);
            }
        }
    }

    public UploadInterface build() {
        return this.uploadInterface;
    }

}
